/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.reactivex.ohos.schedulers;

import io.reactivex.ohos.plugins.RxOhosPlugins;
import io.reactivex.Scheduler;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.concurrent.Callable;

/** HarmonyOS-specific Schedulers. */
public final class OhosSchedulers {

    private static final class MainHolder {
        static final Scheduler DEFAULT
                = new HandlerScheduler(new EventHandler(EventRunner.getMainEventRunner()), false);
    }

    private static final Scheduler MAIN_THREAD = RxOhosPlugins.initMainThreadScheduler(
            new Callable<Scheduler>() {
                @Override public Scheduler call() throws Exception {
                    return MainHolder.DEFAULT;
                }
            });

    /**
     * A {@link Scheduler} which executes actions on the ohos main thread.
     * <p>
     * The returned scheduler will post asynchronous messages to the looper by default.
     *
     * @see #from(EventRunner, boolean)
     */
    public static Scheduler mainThread() {
        return RxOhosPlugins.onMainThreadScheduler(MAIN_THREAD);
    }

    /**
     * A {@link Scheduler} which executes actions on {@code looper}.
     * <p>
     * The returned scheduler will post asynchronous messages to the looper by default.
     *
     * @see #from(EventRunner, boolean)
     */
    public static Scheduler from(EventRunner looper) {
        return from(looper, false);
    }

    /**
     * A {@link Scheduler} which executes actions on {@code looper}.
     *
     *
     */
    public static Scheduler from(EventRunner looper, boolean async) {
        if (looper == null) throw new NullPointerException("looper == null");

        // Below code exists in ohosx-core as well, but is left here rather than include an
        // entire extra dependency.
//        if (Build.VERSION.SDK_INT < 16) {
//            async = false;
//        } else if (async && Build.VERSION.SDK_INT < 22) {
//            // Confirm that the method is available on this API level despite being @hide.
//            Message message = Message.obtain();
//            try {
//                message.setAsynchronous(true);
//            } catch (NoSuchMethodError e) {
//                async = false;
//            }
//            message.recycle();
//        }
        //harmony os Eventhandler 有两种机制。一种自动委托，一种手动委托，自动委托在处理完会自动释放，这里因此不做处理
        return new HandlerScheduler(new EventHandler(looper), async);
    }

    private OhosSchedulers() {
        throw new AssertionError("No instances.");
    }
}
